package com.foreveross.taskservice.common.application.crawlnode.mq.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.jms.core.JmsTemplate;

import com.foreveross.taskservice.common.CacheDataSpace;
import com.foreveross.taskservice.common.TaskTypeConst;
import com.foreveross.taskservice.common.application.crawlnode.mq.ICrawlNodeTaskMQApplication;
import com.foreveross.taskservice.common.bean.AbstractTask;
import com.foreveross.taskservice.common.bean.TaskModel;
import com.foreveross.taskservice.common.model.AbstractTaskModel;
import com.foreveross.taskservice.common.model.airfreight.AirFreightTaskModel;
import com.foreveross.taskservice.common.mq.destination.DestinationFactory;
import com.google.common.collect.Lists;

@SuppressWarnings("deprecation")
public class CrawlNodeTaskMQApplicationImpl implements ICrawlNodeTaskMQApplication{
	private JmsTemplate getTaskJmsTemplate;
	private JmsTemplate rollTaskJmsTemplate;
	@Override
	public AbstractTask getTaskQueueRecordEntity(String channelId) throws Exception {
		AbstractTask[] tasks=getTaskQueueRecordEntitys(null,channelId,1);
		return (tasks==null||tasks.length==0)?null:tasks[0];
	}

	@Override
	public AbstractTask getTaskQueueRecordEntity(String key,
			String channelId) throws Exception {
		AbstractTask[] tasks=getTaskQueueRecordEntitys(key,channelId,1);
		return (tasks==null||tasks.length==0)?null:tasks[0];
	}

	@Override
	public AbstractTask[] getTaskQueueRecordEntitys(String key,String channelId, Integer count) throws Exception {
		AbstractTaskModel[] models=getTaskModels(key, TaskTypeConst.AIRFREIGHT_TASKTYPE, channelId, count);
		if(models!=null && models.length>0){
			AbstractTask[] result=new AbstractTask[models.length];
			for(int i=0;i<models.length;i++){
				AbstractTask task=new TaskModel();
				BeanUtils.copyProperties(models[i], task);
				result[i]=task;
			}
			return result;
		}else{
			return null;
		}
	}

	@Override
	public boolean roolbackTaskQueueRecordEntity(AbstractTask task) throws Exception {
		return rollbackTaskModel(task);
	}



	@Override
	public AbstractTaskModel getTaskModel(String key, int taskType,
			String channelId) throws Exception {
		AbstractTaskModel[] models=getTaskModels(key,taskType,channelId,1);
		return (models==null || models.length==0)?null:models[0];
	}

	@Override
	public AbstractTaskModel[] getTaskModels(String key, int taskType,
			String channelCode, int size) throws Exception {
		List<AbstractTaskModel> models=Lists.newArrayList();
		for(int i=0;i<size;i++){
			AbstractTaskModel model=(AbstractTaskModel) getGetTaskJmsTemplate().receiveAndConvert(
					DestinationFactory.getInstance().getChannelDestination(new CacheDataSpace(taskType), channelCode));
			if(model==null){
				break;
			}else{
				models.add(model);
			}
		}
		return models.toArray(new AbstractTaskModel[0]);
	}

	@Override
	public boolean rollbackTaskModel(AbstractTaskModel taskModel)
			throws Exception {
		if(taskModel==null){
			return false;
		}
		AbstractTaskModel roll;
		if(taskModel instanceof AbstractTask && taskModel instanceof AirFreightTaskModel){//照顾旧模型
			roll=new AirFreightTaskModel();
			BeanUtils.copyProperties(taskModel, roll);
		}else{
			roll=taskModel;
		}
		getRollTaskJmsTemplate().convertAndSend(
				DestinationFactory.getInstance().getRollbackDestination(new CacheDataSpace(roll.getCreateTime(), roll.getTaskType())), 
				roll);
		return true;
	}

	
	
	public JmsTemplate getGetTaskJmsTemplate() {
		return getTaskJmsTemplate;
	}

	public void setGetTaskJmsTemplate(JmsTemplate getTaskJmsTemplate) {
		this.getTaskJmsTemplate = getTaskJmsTemplate;
	}

	public JmsTemplate getRollTaskJmsTemplate() {
		return rollTaskJmsTemplate;
	}

	public void setRollTaskJmsTemplate(JmsTemplate rollTaskJmsTemplate) {
		this.rollTaskJmsTemplate = rollTaskJmsTemplate;
	}
	
	
}
